home *** CD-ROM | disk | FTP | other *** search
- //========================================================================
- // The following routines are provided by Borland Technical Support
- // staff. They are provided as a courtesy and not as part of a Borland
- // product; and, as such, are provided without the assurance of
- // technical support or any specific guarantees.
- //========================================================================
- // Turbo Vision - Screen Saver
- //
- // - This sample code demonstrates one method to saving a screen via
- // Borland Graphics Interface( BGI ) routines.
- //
- // Considerations:
- //
- // 1) Notice the use of getvect() and setvect() which are used
- // to implement a interrupt service routine, in this case, a counter.
- //
- // 2) Notice the path for Initgraph(), you may want to use the bgiobj
- // utility and register bgi driver to explictly link in the driver
- // of your choice. For more information see util.doc.
- //
- // 3) Once the screen saver is invoked, a keyboard hit is needed to
- // restore the tv screen.
- //
- // 4) The #define MINUTES sets the number of minutes the application
- // will wait before invoking the screen saver.
- //
- //==========================================================================
- #define Uses_MsgBox
- #define Uses_TEventQueue
- #define Uses_TEvent
- #define Uses_TProgram
- #define Uses_TApplication
- #define Uses_TKeys
- #define Uses_TRect
- #define Uses_TMenuBar
- #define Uses_TSubMenu
- #define Uses_TMenuItem
- #define Uses_TStatusLine
- #define Uses_TStatusItem
- #define Uses_TStatusDef
- #define Uses_TDeskTop
- #define Uses_TView
- #define Uses_TWindow
- #define Uses_TFrame
- #define Uses_TDialog
- #define Uses_TButton
- #define Uses_TSItem
- #define Uses_TMenu
- #define Uses_TObject
-
-
- #include <stdlib.h> //random
- #include <conio.h> //kbhit
- #include <graphics.h> //graphics...
- #include <dos.h> //getvect, setvect
- #include <tv.h>
-
- #define ONE_MINUTE 1092 // 18.2 * 60 = 1092
- #define MINUTES 1 // minutes till saver kicks-in
- #define PIXEL_COUNT 1000
- #define DELAY_TIME 100 // in milliseconds
-
-
- const int cmAbout = 100;
-
- class TMyApp : public TApplication
- {
- static unsigned counter;
- static void interrupt (*old_1c)(...); // pointer to a function
- int gdriver, gmode, errorcode;
- int i, x, y, color, maxx, maxy,
- maxcolor, seed;
- // which takes variable # of
- public: // arguments and returns nothing
- TMyApp();
- ~TMyApp();
- static TMenuBar *initMenuBar( TRect );
- static TStatusLine *initStatusLine( TRect );
- void handleEvent(TEvent& event);
- void getEvent(TEvent &);
- void screenSaver();
- void doGraphics();
- Boolean graphicsStart();
- void idle();
- static void interrupt new_1c(...); //interrupt function needs to be static
- };
-
-
- //---------------- initialize the static counter -------------------------
- unsigned TMyApp::counter = 0;
-
- //---------------- initialize the static pointer -------------------------
- void interrupt (*TMyApp::old_1c)(...);
-
-
- //-------------------------------------------------------------------------
- // new timer tick isr
- // Increment a counter at each timer tick
- //-------------------------------------------------------------------------
- void interrupt TMyApp::new_1c(...)
- {
- old_1c();
- counter++;
- }
-
-
- //--------------------------------------------------------------------------
- // Overload getEvent: keep setting the counter back to zero
- // as long as events are happening.
- //--------------------------------------------------------------------------
- void TMyApp::getEvent(TEvent &ev)
- {
- TProgram::getEvent(ev);
- if( ev.what != evNothing )
- counter = 0;
- }
-
-
- //--------------------------------------------------------------------------
- // Overload idle to check the counter, call the screen saver
- // after elapsedMinutes. \\
- //--------------------------------------------------------------------------
- void TMyApp::idle()
- {
-
- static unsigned elapsedMinutes = ONE_MINUTE * MINUTES ; // wait time
-
- TProgram::idle();
-
- if( counter == elapsedMinutes )
- {
- screenSaver();
- counter = 0;
- }
-
- }
-
-
- //-----------------------------------------------------------------------------
- // If graphics can start, return True... else return False.
- //-----------------------------------------------------------------------------
- Boolean TMyApp::graphicsStart(void)
- {
- gdriver = DETECT;
-
- initgraph( &gdriver, &gmode, "d:\\bc31\\bgi" );
-
- errorcode = graphresult();
-
- if( errorcode != grOk)
- return False;
- else
- return True;
-
- }
-
-
-
- //----------------------------------------------------------------------------
- // If in graphics mode, call suspend and do the graphics.
- //----------------------------------------------------------------------------
- void TMyApp::screenSaver()
- {
-
-
- if ( !graphicsStart() )
- {
- messageBox("Error Starting Graphics!", mfError );
- }
- else
- {
- suspend();
- doGraphics();
- closegraph();
- resume();
- TProgram::application->redraw();
- }
-
-
- }
-
-
- //----------------------------------------------------------------------------
- // While no keyboard hit, put colored pixels on the screen.
- //----------------------------------------------------------------------------
- void TMyApp::doGraphics()
- {
-
- maxx = getmaxx() + 1;
- maxy = getmaxy() + 1;
- maxcolor = getmaxcolor() + 1;
-
- cleardevice();
- while (!kbhit())
- {
- //seed the random number generator
- seed = random(32767);
- srand(seed);
- for (i=0; i<PIXEL_COUNT; i++)
- {
- x = random(maxx);
- y = random(maxy);
- color = random(maxcolor);
- putpixel(x, y, color);
- }
-
- delay(DELAY_TIME);
- srand(seed);
- for (i=0; i<PIXEL_COUNT; i++)
- {
- x = random(maxx);
- y = random(maxy);
- color = random(maxcolor);
- if (color == getpixel(x, y))
- putpixel(x, y, 0);
- }
- }
-
- }
-
-
- //---------------------------------------------------------------------------
- // TMyApp constructor replaces the original interrupt
- // with our new interrupt
- //---------------------------------------------------------------------------
- TMyApp::TMyApp() :
- TProgInit( &initStatusLine,
- &initMenuBar,
- &initDeskTop
- )
- {
- counter = 0;
- void interrupt (*funcP)(...);
- funcP =( void interrupt(*)(...) )&TMyApp::new_1c;
- old_1c = getvect(0x1C);
- setvect(0x1C, funcP );
-
- }
-
-
- //---------------------------------------------------------------------------
- // TMyApp destructor restores the original interrupt routine
- //---------------------------------------------------------------------------
- TMyApp::~TMyApp()
- {
- setvect(0x1c, old_1c);
- TProgram::~TProgram();
- }
-
-
- void TMyApp::handleEvent(TEvent& event)
- {
-
- TApplication::handleEvent( event );
-
- if( event.what == evCommand )
- {
- switch( event.message.command)
- {
- case cmAbout:
- messageBox("\003Turbo Vision Screen Saver\n\n\003Borland International 1991.",mfInformation);
- break;
-
- default:
- break;
- }
- clearEvent( event );
- }
- }
-
-
- TStatusLine *TMyApp::initStatusLine(TRect r)
- {
- r.a.y = r.b.y - 1;
-
- return new TStatusLine( r,
- *new TStatusDef( 0, 0xFFFF) +
- *new TStatusItem( "~Alt-X~ Exit", kbAltX, cmQuit) +
- *new TStatusItem( "~A~bout Box", kbAltA, cmAbout)
- );
-
- }
-
-
- TMenuBar *TMyApp::initMenuBar( TRect r )
- {
- r.b.y = r.a.y + 1;
- TMenuBar *t;
-
- TMenuItem *two =
- new TMenuItem("~E~xit", cmQuit, kbAltX);
-
- TMenuItem *one =
- new TMenuItem("~\xF0~", kbAltSpace,
- new TMenu( *new TMenuItem("~A~bout", cmAbout, kbAltA)),
- hcNoContext, two);
-
- return ( new TMenuBar( r, new TMenu( *one ) ) );
-
-
- }
-
-
- int main()
- {
-
- TMyApp myApp;
- myApp.run();
- return 0;
- }
-